﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SQLCheck
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }
        private void MainFrm_Load(object sender, EventArgs e)
        {
            lbList.DrawItem += new DrawItemEventHandler(drawLog);

            lbList.DisplayMember = "Name";
            lbList.ValueMember = "Value";
           // txtFolder.Text = "F:\医院物流管理系统\05 医院物流管理系统(改进版)\02 代码类\01 源代码\08 SPD耗材物流管理系统\QuickShare.MaterialSpd.API-日照分支\QuickShare.MaterialSpd.Web\SqlScripts\MSPD\6.Proc";
        }

        private void drawLog(Object sender, DrawItemEventArgs e)
        {
            //if (lbList.Items.Count > 0 && lbList.Items.Count > e.Index)
            //{
            //    if (e.Index < 0)
            //    { return; }
            //    string str = lbList.Items[e.Index].ToString();

            //    Color foreColor = Color.Black;
            //    //重点，根据item的具体内容决定颜色
            //    if (str.Contains("EJK")) { foreColor = Color.Green; }
            //    if (str.Contains("YJK")) { foreColor = Color.Red; }
            //    //重点，画上颜色
            //    e.Graphics.DrawString(str, e.Font, new SolidBrush(foreColor), e.Bounds);
            //}
            Brush FontBrush = null;
            ListBox listBox = sender as ListBox;
            if (e.Index > -1)
            {
                var model = listBox.Items[e.Index] as ShowModel;
                if (model.IsRead)
                {
                    FontBrush = Brushes.Green;
                }
                else
                    FontBrush = Brushes.Red;
                //switch (.ToString())
                //{
                //    case "Critical": FontBrush = Brushes.Brown; break;
                //    case "Major": FontBrush = Brushes.Red; break;
                //    case "Minor": FontBrush = Brushes.Orange; break;
                //    case "Warning": FontBrush = Brushes.Yellow; break;
                //    default: FontBrush = Brushes.Green; break;
                //}
                e.DrawBackground();
                e.Graphics.DrawString(listBox.Items[e.Index].ToString(), e.Font, FontBrush, e.Bounds);
                e.DrawFocusRectangle();
            }
        }

        private void lbList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var showModel = lbList.SelectedItem as ShowModel;
            string path = txtFolder.Text.ToString() + showModel.Value;

            var model = showModelList.First(p => p.Value == showModel.Value);
            model.IsRead = true;

            FileShow fileShow = new FileShow(
            path,
            redFile[showModel.Value]
            );
            fileShow.ShowDialog();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.SelectedPath = txtFolder.Text;
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                txtFolder.Text = folderBrowserDialog.SelectedPath;
            }
        }

        List<string> Files = new List<string>();

        Dictionary<string, Dictionary<string, List<string>>> redFile = new Dictionary<string, Dictionary<string, List<string>>>();
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtFolder.Text))
            {
                MessageBox.Show("请先选择文件夹");
                return;
            }
            Files = System.IO.Directory.GetFiles(txtFolder.Text).ToList();
            if (Files.Count() == 0)
            {
                WriteMsg("没有文件");
                return;
            }
            redFile.Clear();
            Task.Run(() =>
            {
                int i = 1;
                foreach (var item in Files)
                {
                    WriteMsg($"{i}/{Files.Count} 解析文件：{item}");
                    Check check = new Check(item, cbALL.Checked);
                    var res = check.Start();
                    if (res.Count > 0)
                    {
                        redFile.Add(item.Replace(txtFolder.Text, ""), res);
                    }
                    i++;
                }

                SetListBox();
                WriteMsg($"总共【{redFile.Count}】文件【{redFile.SelectMany(p => p.Value).Count()}】语句【{redFile.SelectMany(p => p.Value).SelectMany(p => p.Value).Count()}】处需要修改");
            });


        }

        List<ShowModel> showModelList = new List<ShowModel>();
        private void SetListBox()
        {
            showModelList.Clear();
            foreach (var item in redFile)
            {
                showModelList.Add(new ShowModel
                {
                    Name = $"【{item.Value.Count}】{item.Key}",
                    Value = item.Key
                });
            }
            if (lbList.InvokeRequired)
            {
                lbList.Invoke(new Action<List<ShowModel>>(p =>
                {
                    lbList.Items.Clear();
                    foreach (var item in p)
                    {
                        lbList.Items.Add(item);
                    }
                    //lbList.DataSource = p;
                }), showModelList);
            }
            else
            {
                /*lbList.DataSource = showModelList*/
                ;
                lbList.Items.Clear();
                foreach (var item in showModelList)
                {
                    lbList.Items.Add(item);
                }
            }
        }

        /// <summary>
        /// 设置消息
        /// </summary>
        /// <param name="msg"></param>
        private void WriteMsg(string msg)
        {
            if (lbMsg.InvokeRequired)
            {
                lbMsg.Invoke(new Action<string>(p =>
                {
                    lbMsg.Text = p;
                }), msg);
            }
            else
                lbMsg.Text = msg;
        }


    }

    public class ShowModel
    {
        public string Name { get; set; }
        public string Value { get; set; }

        public bool IsRead { get; set; } = false;

        public override string ToString()
        {
            return Name;
        }
    }
}
